package com.mhc.suzuki.controller;

import com.mhc.haval.security.model.User;
import com.mhc.suzuki.bo.PaymentDetailBO;
import com.mhc.suzuki.bo.PaymentNotificationQueryBO;
import com.mhc.suzuki.bo.SubmitTaskBO;
import com.mhc.suzuki.constant.ErrorCodeEnum;
import com.mhc.suzuki.form.PaymentNotificationQueryForm;
import com.mhc.suzuki.form.SubmitTaskForm;
import com.mhc.suzuki.service.PaymentNotificationService;
import com.mhc.suzuki.util.AmmountUtil;
import com.mhc.suzuki.util.DateUtil;
import com.mhc.suzuki.vo.PaymentDetailVO;
import com.mhc.suzuki.vo.PaymentRequestListVO;
import com.subaru.common.entity.BizResult;
import com.subaru.common.util.BeanCopierUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by HW
 * on 2017/8/29 13:52.
 * 打款申请对应controller
 */
@Slf4j
@RestController
@RequestMapping(value = "/paymentRequest")
public class PaymentNotificationController {


    @Autowired
    private PaymentNotificationService paymentNotificationService;


    /**
     * 打款通知列表获取
     *
     * @param paymentNotificationQueryForm
     * @return
     */
    @ApiOperation(value = "获取打款通知列表", notes = "根据查询条件获取征信列表")
    @RequestMapping(value = {"/list"}, method = RequestMethod.GET)
    public BizResult<List<PaymentRequestListVO>> getPaymentRequestList(@AuthenticationPrincipal User user, @ModelAttribute PaymentNotificationQueryForm paymentNotificationQueryForm) {
        BizResult bizResult;

        //接收查询条件
        PaymentNotificationQueryBO paymentNotificationQueryBO = new PaymentNotificationQueryBO();
        BeanCopierUtil.copy(paymentNotificationQueryForm, paymentNotificationQueryBO);
        if (StringUtils.isNotBlank(paymentNotificationQueryForm.getGmtStart()) && StringUtils.isNotBlank(paymentNotificationQueryForm.getGmtEnd())) {
            String start = paymentNotificationQueryForm.getGmtStart();
            start += " 00:00:00";
            String end = paymentNotificationQueryForm.getGmtEnd();
            end += " 23:59:59";
            LocalDateTime s = DateUtil.defaultParseStringToDateTime(start);
            LocalDateTime e = DateUtil.defaultParseStringToDateTime(end);
            paymentNotificationQueryBO.setGmtStart(s);
            paymentNotificationQueryBO.setGmtEnd(e);
        }

        //查询列表
        List<PaymentNotificationQueryBO> paymentNotificationQueryBOS = null;
        int totalCount = 0;
        log.info(paymentNotificationQueryBO.toString());
        paymentNotificationQueryBO.setStaffId(user.getStaffId());//设置员工id
        //列表查询
        paymentNotificationQueryBOS = paymentNotificationService.queryPaymentNotificationQueryBOSByQuery(paymentNotificationQueryBO);

        //总条数查询
        totalCount = paymentNotificationService.queryTotalCount(paymentNotificationQueryBO);

        /*BO-->VO*/
        List<PaymentRequestListVO> paymentRequestListVOS = new ArrayList<>();
        Iterator<PaymentNotificationQueryBO> iterator = paymentNotificationQueryBOS.iterator();
        while(iterator.hasNext()){
            PaymentNotificationQueryBO p = iterator.next();
            PaymentRequestListVO paymentRequestListVO = new PaymentRequestListVO();
            BeanCopierUtil.copy(p, paymentRequestListVO);
            paymentRequestListVOS.add(paymentRequestListVO);
        }
        bizResult = BizResult.create(paymentRequestListVOS, true, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
        bizResult.setPageNo(paymentNotificationQueryBO.getPageNo());
        bizResult.setPageSize(paymentNotificationQueryBO.getPageSize());
        bizResult.setTotalCount(totalCount);
        return bizResult;
    }


    /**
     * 获取打款通知, GPS详情
     * @param orderNo
     * @return
     */
    @ApiOperation(value = "获取打款通知详情", notes = "根据订单号获取打款通知详情")
    @RequestMapping(value = {"/detail/{orderNo}"}, method = RequestMethod.GET)
    public BizResult<PaymentDetailVO> getPaymentRequestDetail(@PathVariable String orderNo) {


        if (StringUtils.isBlank(orderNo)) {
            return BizResult.create(ErrorCodeEnum.ORDER_NO_ISNULL.getErrCode(), ErrorCodeEnum.ORDER_NO_ISNULL.getErrMsg());
        }

        PaymentDetailBO paymentDetailBO =  paymentNotificationService.queryPaymentDetailByOrderNo(orderNo);
        if(paymentDetailBO != null){
            PaymentDetailVO paymentDetailVO = new PaymentDetailVO();
            BeanCopierUtil.copy(paymentDetailBO, paymentDetailVO);
            //金额大写格式转换
            String amount = AmmountUtil.numberToCN(paymentDetailVO.getLoaningAmount());
            paymentDetailVO.setUpcaseLoanAmount(amount);
            return BizResult.create(paymentDetailVO, true, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
        } else {
            return BizResult.create(ErrorCodeEnum.NO_DATA.getErrCode(), ErrorCodeEnum.NO_DATA.getErrMsg());
        }

    }




    /**
     * 财务打款操作
     * @param submitTaskForm
     * @param logedUser
     * @return
     */
    @ApiOperation(value = "财务打款操作", notes = "根据订单号更改打款状态")
    @RequestMapping(value = {"/notification"}, method = RequestMethod.POST)
    @ApiImplicitParam(name = "submitTaskForm", value = "提交审核信息", required = false,paramType = "body",dataType = "SubmitTaskForm")
    public BizResult remittance(@RequestBody SubmitTaskForm submitTaskForm,@AuthenticationPrincipal User logedUser) {

        if (StringUtils.isNotBlank(submitTaskForm.getOrderNo())){
            SubmitTaskBO submitTaskBO = new SubmitTaskBO();
            BeanUtils.copyProperties(submitTaskForm,submitTaskBO);
            PaymentDetailBO paymentDetailBO = new PaymentDetailBO();
            paymentDetailBO.setOrderNo(submitTaskBO.getOrderNo());
            paymentDetailBO.setHandleStaffId(logedUser.getStaffId());
            if(paymentNotificationService.remittance(paymentDetailBO,submitTaskBO,logedUser)){
                return BizResult.create(Boolean.TRUE, true, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
            }
        }
        return BizResult.create(ErrorCodeEnum.PARAM_ERROR.getErrCode(), ErrorCodeEnum.PARAM_ERROR.getErrMsg());

    }




}
